# sed -i '$a\\nsource /home/wj/vimrc/linux_config/04_config_fzf.sh' /home/wj/.bashrc

alias viupdate="bash ~/vimrc/update_vi.sh"
alias tclsh="rlwrap -c tclsh"
alias loadise="source /opt/Xilinx/14.7/ISE_DS/settings64.sh && export LD_PRELOAD=/opt/Xilinx/usb-driver/libusb-driver.so"
alias load_vivado_2018_3="source /opt/Xilinx/Vivado/2018.3/settings64.sh && source /opt/Xilinx/SDK/2018.3/settings64.sh"
alias load_vivado_2020_2="source /opt/Xilinx/Vivado/2020.2/settings64.sh && source /opt/Xilinx/Vitis/2020.2/settings64.sh"
alias load_vivado_2021_2="source /opt/Xilinx/Vivado/2021.2/settings64.sh && source /opt/Xilinx/Vitis/2021.2/settings64.sh"
alias ls="eza --icons=auto -b"
alias cat="batcat"
alias intellij="source /home/wj/program/idea-IC-231.9423.9/bin/idea.sh"
export MANPAGER='nvim +Man!'

# eval "$(lua $HOME/.config/nvim/z.lua  --init bash)"

# apt get 安装版本老旧，导致Fuzzy completion不能使用
# 使用git下载最新版本

# -d file, 检查file是否存在并且是一个目录
if [ ! -d ~/.fzf ] 
then
    git clone --depth 1 git@github.com:junegunn/fzf.git ~/.fzf
    ~/.fzf/install

    # 检查.bashrc文件是否存在字符串"04_config_fzf", 如果不存在，则添加到文件尾部
    if ! grep -q "04_config_fzf" ~/.bashrc; then
        echo "source ~/vimrc/linux_config/04_config_fzf.sh" >> ~/.bashrc
    fi

fi


# export FZF_DEFAULT_COMMAND='rg --files'
# export FZF_COMPLETION_TRIGGER='.'

# fkill - kill processes - list only the ones you can kill. Modified the earlier script.
fkill() {
    local pid 
    if [ "$UID" != "0" ]; then
        pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
    else
        pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
    fi  

    if [ "x$pid" != "x" ]
    then
        echo $pid | xargs kill -${1:-9}
    fi  
}

export EDITOR=nvim

# Open the selected file with the default editor
fe() {
  IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
  [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
}

# cda - Starting from the root directory, search all directories, including hidden directories
cda() {
  local dir
  # dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
  dir=$(find ${1:-~} -type d 2> /dev/null | fzf +m) && cd "$dir"
}

cdl() {
  local dir
  dir=$(find ${1:-.} -path '*/\.*' -prune \
                  -o -type d -print 2> /dev/null | fzf +m) &&
  cd "$dir"
}

# source ~/vimrc/linux_config/fzf_marks.sh

#   __fzf_complete_pdf() {
#       rg --files -t pdf | sort -u
#   }
#   
#   _fzf_complete_pdf() {
#     _fzf_complete +m -- "$@" < <(__fzf_complete_pdf "")
#   }
#   
#   _fzf_complete_pdf_notrigger() {
#       FZF_COMPLETION_TRIGGER='' _fzf_complete_pdf
#   }
#   # alias foxit="FoxitReader"
#   complete -o bashdefault -o default -F _fzf_complete_pdf_notrigger masterpdfeditor5
